﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class ClubInfo : Form
    {
        private int clubId = 0;
        private int fk_coach = 0;
        private int fk_stadion = 0;

        public ClubInfo(int _id)
        {
            InitializeComponent();
            clubId = _id;
        }

        private void UpdatePlayerList()
        {
            dgvPlayers.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getPlayersOfClub]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvPlayers.Rows.Add(dataReader["id"], dataReader["photo"], dataReader["name"], dataReader[3], dataReader[4]);
            }
            dataReader.Close();
            conn.Close();

            dgvPlayers.ClearSelection();

        }

        private void ClubInfo_Load(object sender, EventArgs e)
        {
            if (clubId != 0)
            {
                UpdatePlayerList();

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[getClubInfo]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;
                SqlDataReader dataReader = myCommand.ExecuteReader();
                if (dataReader.Read())
                {
                    if (!DBNull.Value.Equals(dataReader[0]))
                        clubLabel.Image = ImageUtils.ByteArrayToImage((byte[])dataReader[0]);

                    clubName.Text = Convert.ToString(dataReader[1]);

                    fk_coach = Convert.ToInt32(dataReader[2]);

                    if (!DBNull.Value.Equals(dataReader[3]))
                        coachPhoto.Image = ImageUtils.ByteArrayToImage((byte[])dataReader[3]);

                    coachName.Text = Convert.ToString(dataReader[4]);

                    fk_stadion = Convert.ToInt32(dataReader[5]);

                    stadionName.Text = Convert.ToString(dataReader[6]) + " г." + Convert.ToString(dataReader[7]);

                }
                dataReader.Close();
                conn.Close();
            }

            else {             
                save.Location = new Point(254,384);
                this.Size = new Size(360, 447);
            }
            
        }

        
        private void clubLabel_Click(object sender, EventArgs e)
        {
            ImageUtils.setImageFromStream(clubLabel);            
        }

        private void stadionEdit_Click(object sender, EventArgs e)
        {
            StadionInfo si = new StadionInfo(fk_stadion);
            si.ShowDialog();
            fk_stadion = si.stadionId;

            stadionName.Text = si.stadionName + " г." + si.cityName;

            setSaveState();
        }

        private void UpdateCoach()
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getPerson]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = fk_coach;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                if (!DBNull.Value.Equals(dataReader["photo"]))
                {
                    coachPhoto.Image = ImageUtils.ByteArrayToImage((byte[])dataReader["photo"]);
                }
                coachName.Text = Convert.ToString(dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();

        }

        private void coachEdit_Click(object sender, EventArgs e)
        {
            CoachEdit ce = new CoachEdit(clubId, fk_coach);
            ce.ShowDialog();
            fk_coach = ce.coachId;

            UpdateCoach();
            UpdatePlayerList();

            setSaveState();
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (clubId == 0)
            {
                //addClub
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addClub]";

                if (clubLabel.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(clubLabel.Image);
                    myCommand.Parameters.Add("@label", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@label", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, coachName.Text.Length).Value = Convert.ToString(clubName.Text);

                if (fk_coach != 0)
                {
                    myCommand.Parameters.Add("@fk_coach", SqlDbType.Int).Value = fk_coach;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_coach", SqlDbType.Int).Value = DBNull.Value;
                }

                if (fk_stadion != 0)
                {
                    myCommand.Parameters.Add("@fk_stadion", SqlDbType.Int).Value = fk_stadion;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_stadion", SqlDbType.Int).Value = DBNull.Value;
                }

                myCommand.ExecuteScalar();
                conn.Close();
            }
            else
            {
                //editClub
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[editClub]";

                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;

                if (clubLabel.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(clubLabel.Image);
                    myCommand.Parameters.Add("@label", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@label", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, coachName.Text.Length).Value = Convert.ToString(clubName.Text);

                if (fk_coach != 0)
                {
                    myCommand.Parameters.Add("@fk_coach", SqlDbType.Int).Value = fk_coach;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_coach", SqlDbType.Int).Value = DBNull.Value;
                }

                if (fk_stadion != 0)
                {
                    myCommand.Parameters.Add("@fk_stadion", SqlDbType.Int).Value = fk_stadion;
                }
                else
                {
                    myCommand.Parameters.Add("@fk_stadion", SqlDbType.Int).Value = DBNull.Value;
                }

                myCommand.ExecuteScalar();
                conn.Close();
            }

            this.Close();
        }

        private void dgvPlayers_SelectionChanged(object sender, EventArgs e)
        {

            if (dgvPlayers.SelectedRows.Count > 0)
            {
                edit.Enabled = true;
                del.Enabled = true;
            }
            else {
                edit.Enabled = false;
                del.Enabled = false;
            }

        }

        private void add_Click(object sender, EventArgs e)
        {
            PlayerInfo pi = new PlayerInfo(0,clubId);
            pi.ShowDialog();

            UpdatePlayerList();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            int plId = Convert.ToInt32(dgvPlayers.SelectedRows[0].Cells["id"].Value);
            PlayerInfo pi = new PlayerInfo(plId, clubId);
            pi.ShowDialog();

            UpdatePlayerList();
        }

        private void del_Click(object sender, EventArgs e)
        {
            int plId = Convert.ToInt32(dgvPlayers.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delPlayer]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = plId;

            conn.Open();
            myCommand.ExecuteScalar();
            conn.Close();

            UpdatePlayerList();
        }

        private void setSaveState() {

            if ((clubName.Text.Length > 0) && (fk_coach != 0) && (fk_stadion != 0))
            {
                save.Enabled = true;
            }
            else {
                save.Enabled = false;
            }
        
        }

        private void clubName_TextChanged(object sender, EventArgs e)
        {
            setSaveState();
        }
    }
}
